package com.campus.dao.impl;

import com.campus.dao.TeachingCallingDetailDao;
import com.campus.dto.TeachingCallingDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class TeachingCallingDetailDaoImpl implements TeachingCallingDetailDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int addCallingDetail(TeachingCallingDetail detail) {
        String sql = "INSERT INTO t_teaching_calling_detail (stu_in_class_id, call_id, answer_time, answer_result, no_answer_reason, operator_teacher) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                detail.getStuInClassId(),
                detail.getCallId(),
                detail.getAnswerTime(),
                detail.getAnswerResult(),
                detail.getNoAnswerReason(),
                detail.getOperatorTeacher());
    }

    @Override
    public int deleteCallingDetailById(int id) {
        String sql = "DELETE FROM t_teaching_calling_detail WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateCallingDetail(TeachingCallingDetail detail) {
        String sql = "UPDATE t_teaching_calling_detail SET stu_in_class_id = ?, call_id = ?, answer_time = ?, answer_result = ?, no_answer_reason = ?, operator_teacher = ? " +
                "WHERE id = ?";
        return jdbcTemplate.update(sql,
                detail.getStuInClassId(),
                detail.getCallId(),
                detail.getAnswerTime(),
                detail.getAnswerResult(),
                detail.getNoAnswerReason(),
                detail.getOperatorTeacher(),
                detail.getId());
    }

    @Override
    public List<TeachingCallingDetail> findAllCallingDetails() {
        String sql = "SELECT * FROM t_teaching_calling_detail";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TeachingCallingDetail.class));
    }

}
